<template></template>

<script setup>
	import {
		onMounted,
		reactive,
		ref,
		provide,
		watch,
		onUnmounted
	} from "vue";

	import {
		useCounterStore
	} from "@/stores/counter.js";

	import {
		onLaunch,
		onShow,
		onHide
	} from "@dcloudio/uni-app";
	import {
		useUserStore
	} from "@/stores/userStore";

	const st = useCounterStore();

	const userStore = useUserStore();

	onLaunch(() => {
		// 检查登录状态
		checkLoginStatus();
	});

	const checkLoginStatus = () => {
		// 获取本地存储的用户信息
		const userInfo = uni.getStorageSync("userInfo");

		if (!userInfo) {
			// 如果没有用户信息，跳转到登录页
			const currentPage = getCurrentPages();
			const currentRoute = currentPage[currentPage.length - 1]?.route;

			// 如果当前不在登录或注册页面，则跳转到登录页
			if (
				currentRoute !== "pages/other/login" &&
				currentRoute !== "pages/other/register"
			) {
				uni.reLaunch({
					url: "/pages/other/login",
				});
			}
		} else {
			// 有用户信息，更新 store
			try {
				// 检查 userInfo 是否已经是对象
				const parsedUserInfo = typeof userInfo === 'string' ? JSON.parse(userInfo) : userInfo;
				userStore.updateUserInfo(parsedUserInfo);
			} catch (error) {
				console.error('Error updating user info:', error);
				// 如果解析失败，清除可能损坏的数据
				uni.removeStorageSync("userInfo");
			}
		}
	};
</script>

<script>
	export default {
		onLaunch: function() {
			console.log("App Launch");
		},
		onShow: function() {
			console.log("App Show");
		},
		onHide: function() {
			console.log("App Hide");
		},
		// 注册微信小程序中需要使用的Kivicube组件
		// 仅在微信小程序平台中使用
		usingComponents: {
			'kivicube-scene': 'plugin://kivicube/kivicube-scene'
		}
	};
	// .uni-page-head {
	// 	padding: 30px
	// }
</script>
<style lang="scss">
	@import "@/uni_modules/uni-scss/index.scss";
	@import "@/assets/css/responsive.scss"; // 导入响应式样式文件

	// @import "@/uni.scss";
	page {
		--td-brand-color: #6c8380;
		--td-brand-color-1: #f2f3ff;
		--td-brand-color-2: #d9e1ff;
		--td-brand-color-3: #b5c7ff;
		--td-brand-color-4: #8eabff;
		--td-brand-color-5: #618dff;
		--td-brand-color-6: #6c8380;
		--td-brand-color-7: #6c8380;
		--td-brand-color-8: #6c8380;
		--td-brand-color-9: #6c8380;
		--td-brand-color-10: #6c8380;
		--td-bg-color: #b6ad8c;
		--td-bg-color-1: #e7e3d9;
		background-color: #f5f5f5;
	}
</style>